﻿using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet.Ajax
{
	public class ActionButtonItem : Nitobi.ButtonItem, IActionTrigger
	{
		protected string m_actions = null;

		protected string m_bizprocActions = null;
		public string Actions
		{
			get { string s = (string)State.State.get("actions"); return s == null ? m_bizprocActions : s; }
			set { m_bizprocActions = value; if (BPC != null)State.State.put("actions", value); }
		}
		protected WidgetOfflineState State
		{
			get
			{
				WidgetOfflineState state = BPC.getWidgetOfflineState(WidgetId);
				if (state == null)
				{
					BPC.addWidgetOfflineState(state = new WidgetOfflineState(WidgetId, this.GetType().AssemblyQualifiedName));
					state.BizProcWidgetType = BizProcWidgetType;
					if (state.State.get("boolSwitch") == null)
					{
						//state.State.put("boolSwitch", m_sessionBoolSwitch);
						//state.State.put("hideOnDisable", m_hideOnDisabled);
						//if (m_popupPage != null)
						//    state.State.put("popupPage", m_popupPage);
						//state.State.put("m_popupWidth", m_popupWidth);
						//state.State.put("m_popupHeight", m_popupHeight);
						//state.State.put("m_popupTitle", m_popupTitle);
						//state.State.put("m_popupParam", m_popupParam);
						//state.State.put("actions", Actions);
						//if (m_specifiedTriggerParam != null)
						//    state.State.put("m_specifiedTriggerParam", m_specifiedTriggerParam);
					}
					setupParentBizProcs();
				}
				return state;
			}
		}

		protected void setupParentBizProcs()
		{
			if (m_bizprocActions != null)
			{
				State.ParentBizProcIds.Clear();
				string[] bizprocids = m_bizprocActions.Split(',');
				foreach (string bizprocid in bizprocids)
				{
					string[] actionInfo = bizprocid.Split('.');
					if (actionInfo.Length == 2)
						State.ParentBizProcIds.Add(actionInfo[0]);
					else
						throw new BaseException(string.Format("Invalid ActionButton.Actions property format: {0}", m_bizprocActions));
				}
			}
		}


		public BizProcCoordinator BPC
		{
			get { object Page = null; return (Page is IPage) ? ((IPage)Page).BPC : null; }
		}


		#region IActionTrigger Members


		public void setupStateFromActions()
		{
			throw new NotImplementedException();
		}

		#endregion

		#region IWidget Members

		public BizProcWidgetType BizProcWidgetType
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		public string ParentBizProcIds
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		public bool Visible
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		public string WidgetId
		{
			get { throw new NotImplementedException(); }
		}

		#endregion
	}
}
